What is benchmark?
The 'benchmark' npm package is a library for benchmarking JavaScript code. It provides a simple and flexible API to measure the performance of code snippets, allowing developers to compare the speed of different implementations and optimize their code.
What are benchmark's main functionalities?
Basic Benchmarking
This feature allows you to create a suite of benchmarks to compare the performance of different code snippets. The example demonstrates how to add tests, attach listeners for cycle and completion events, and run the suite asynchronously.
const Benchmark = require('benchmark');
const suite = new Benchmark.Suite;
// add tests
suite.add('RegExp#test', function() {
/o/.test('Hello World!');
})
.add('String#indexOf', function() {
'Hello World!'.indexOf('o') > -1;
})
// add listeners
.on('cycle', function(event) {
console.log(String(event.target));
})
.on('complete', function() {
console.log('Fastest is ' + this.filter('fastest').map('name'));
})
// run async
.run({ 'async': true });
Event Handling
This feature allows you to handle various events during the benchmarking process, such as 'start', 'cycle', 'complete', etc. The example shows how to attach event listeners to a benchmark suite.
const Benchmark = require('benchmark');
const suite = new Benchmark.Suite;
suite.add('Example Test', function() {
// code to benchmark
})
.on('start', function() {
console.log('Benchmark started');
})
.on('complete', function() {
console.log('Benchmark completed');
})
.run();
Customizing Benchmark Options
This feature allows you to customize various options for your benchmarks, such as the minimum number of samples. The example demonstrates how to set the 'minSamples' option for a benchmark test.
const Benchmark = require('benchmark');
const suite = new Benchmark.Suite;
suite.add('Example Test', function() {
// code to benchmark
}, {
'minSamples': 100
})
.run();
Other packages similar to benchmark
fastest
The 'fastest' package is another benchmarking tool for JavaScript. It focuses on providing a simple API for running benchmarks and comparing the performance of different code snippets. Compared to 'benchmark', 'fastest' is more lightweight and may be easier to use for simple benchmarking tasks.
matcha
The 'matcha' package is a powerful benchmarking library for Node.js. It provides a command-line interface and a flexible API for writing and running benchmarks. 'matcha' offers more advanced features and a more comprehensive reporting system compared to 'benchmark'.
benchmarkjs
The 'benchmarkjs' package is a fork of the 'benchmark' library with additional features and improvements. It aims to provide a more modern and feature-rich benchmarking tool while maintaining compatibility with the original 'benchmark' API.
Benchmark.js v2.1.4
A robust benchmarking library that supports high-resolution timers & returns statistically significant results. As seen on jsPerf.
Documentation
Download
Installation
Benchmark.js’ only hard dependency is lodash.
Include platform.js to populate Benchmark.platform.
In a browser:
<script src="lodash.js"></script>
<script src="platform.js"></script>
<script src="benchmark.js"></script>
In an AMD loader:
require({
'paths': {
'benchmark': 'path/to/benchmark',
'lodash': 'path/to/lodash',
'platform': 'path/to/platform'
}
},
['benchmark'], function(Benchmark) {});
Using npm:
$ npm i --save benchmark
In Node.js:
var Benchmark = require('benchmark');
Optionally, use the microtime module by Wade Simmons:
npm i --save microtime
Usage example:
var suite = new Benchmark.Suite;
suite.add('RegExp#test', function() {
/o/.test('Hello World!');
})
.add('String#indexOf', function() {
'Hello World!'.indexOf('o') > -1;
})
.on('cycle', function(event) {
console.log(String(event.target));
})
.on('complete', function() {
console.log('Fastest is ' + this.filter('fastest').map('name'));
})
.run({ 'async': true });
Support
Tested in Chrome 54-55, Firefox 49-50, IE 11, Edge 14, Safari 9-10, Node.js 6-7, & PhantomJS 2.1.1.
BestieJS
Benchmark.js is part of the BestieJS “Best in Class” module collection. This means we promote solid browser/environment support, ES5+ precedents, unit testing, & plenty of documentation.